#include <iostream>
#include "XJThreadSpecific.h"
#include "XJThreadManager.h"
#include <unistd.h>


typedef XJUtilClass::XJThreadSpecific<int> xjts_int;
typedef XJUtilClass::XJThreadSpecific<std::string> xjts_str;
typedef XJUtilClass::XJThreadManager::XJThreadRunState XJThreadRunState;
int thread_fun_1(XJThreadRunState s,void *) {
	if (s == XJThreadRunState::R_S_BEGIN) {
		int *p = xjts_int::get();
		std::cout << __func__  << " R_S_BEGIN " << p << std::endl;
		if (p == 0) {
			xjts_int::set(new int (10));
			xjts_str::set(new std::string(__func__));
		}
		return 0;
	}
	if (s == XJThreadRunState::R_S_END) {
		std::cout << __func__  << " R_S_END "<< std::endl;
		xjts_int::set(0);
		xjts_str::set(0);
		return 0;
	}
	std::cout  << __func__  << " R_S_RUNING " << *xjts_int::get() << std::endl;
	std::cout  << __func__  << " R_S_RUNING " << *xjts_str::get() << std::endl;
	sleep(1);
	return 0;
}

int thread_fun_2 (XJThreadRunState s, void *) {
	if (s == XJThreadRunState::R_S_BEGIN) {
		int *p = xjts_int::get();
		std::cout << __func__  << " R_S_BEGIN " << p << std::endl;
		if (p == 0) {
			xjts_int::set(new int (20));
			xjts_str::set(new std::string(__func__));
		}
		return 0;
	}
	if (s == XJThreadRunState::R_S_END) {
		std::cout << __func__  << " R_S_END "<< std::endl;
		xjts_int::set(0);
		xjts_str::set(0);
		return 0;
	}
	std::cout  << __func__  << " R_S_RUNING " << *xjts_int::get() << std::endl;
	std::cout  << __func__  << " R_S_RUNING " << *xjts_str::get() << std::endl;
	sleep(1);
	return 0;
}
